package com.zs.propertymanage.service.impl.impl;

import com.zs.propertymanage.dataObj.DTO;
import com.zs.propertymanage.dataObj.GeneralExtra;
import com.zs.propertymanage.dataObj.R;
import com.zs.propertymanage.dataObj.entity.Manager;
import com.zs.propertymanage.mapper.ManagerMapper;
import com.zs.propertymanage.service.impl.ManagerService;
import com.zs.propertymanage.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public R getAllManagerWithPlain() {
        List<Manager> managerList = managerMapper.getAllManagerWithPlain();
        return managerList == null ? R.error("查询管理员列表失败") : R.ok("查询管理员列表成功", managerList);
    }

    @Override
    public R getAllManagerWithCondition(String search, Integer currentPage, Integer pageSize) {
        Integer start = (currentPage - 1) * pageSize;
        List<Manager> managerList = managerMapper.getAllManagerWithCondition(search, start, pageSize);
        Integer total = managerMapper.getCountWithCondition(search, start, pageSize);
        DTO<Manager> managerDto = new DTO<>(managerList, total);
        return total == 0 ? R.error("未查询到管理员") : R.ok("查询管理员成功", managerDto);
    }

    @Override
    public R addManager(Manager manager) {
        String pwd = manager.getPassword();
        // 密码MD5加密
        if (pwd != null && !pwd.equals("")) {
            manager.setPassword(MD5Util.MD5Encrypt(pwd));
        } else {
            return R.error("密码不能为空");
        }
        manager.setId(UUID.randomUUID().toString());
        Integer result = managerMapper.addManager(manager);
        return result == 1 ? R.ok("添加管理员成功") : R.error("添加管理员失败");
    }

    @Override
    public R updateManager(Manager manager) {
        Integer result = managerMapper.updateManager(manager);
        return result == 1 ? R.ok("修改管理员成功") : R.error("修改管理员失败");
    }

    @Override
    public R deleteManagerById(String id) {
        Integer result = managerMapper.deleteManagerById(id);
        return result == 1 ? R.ok("删除管理员成功") : R.error("删除管理员失败");
    }

    @Override
    public R getAllManagerRole() {
        List<GeneralExtra> managerRoleList = managerMapper.getAllManagerRole();
        return managerRoleList == null ? R.error("查询管理员角色列表失败") : R.ok("查询管理员角色列表成功", managerRoleList);
    }

    @Override
    public R addManagerRole(String roleName) {
        if (roleName == null || roleName.equals(""))
            return R.error("添加失败，管理员角色名不能为空");
        Integer result = managerMapper.addManagerRole(roleName);
        return result == 1 ? R.ok("添加管理员角色成功") : R.error("添加管理员角色失败");
    }

    @Override
    public R deleteManagerRoleById(Integer id) {
        Integer result = managerMapper.deleteManagerRoleById(id);
        return result == 1 ? R.ok("删除管理员角色成功") : R.error("删除管理员角色失败");
    }

    @Override
    public R getManagerById(String id) {
        Manager manager = managerMapper.getManagerById(id);
        return manager == null ? R.error("查询管理员信息失败") : R.ok("查询管理员信息成功", manager);
    }

}
